﻿// This is an open source non-commercial project. Dear PVS-Studio, please check it.
// PVS-Studio Static Code Analyzer for C, C++ and C#: http://www.viva64.com

// ReSharper disable CheckNamespace
// ReSharper disable CommentTypo
// ReSharper disable IdentifierTypo
// ReSharper disable InconsistentNaming
// ReSharper disable UnusedMember.Global

/* Standard2of5.cs -- штрих-код "2 из 5"
 * Ars Magna project, http://arsmagna.ru
 */

#region Using directives

using AM.Text;

#endregion

#nullable enable

namespace AM.Drawing.Barcodes;

/// <summary>
/// Штрих-код "2 из 5"
/// </summary>
public sealed class Standard2of5
    : LinearBarcodeBase
{
    #region Private members

    private static readonly string[] _patterns =
    {
        "10101110111010",
        "11101010101110",
        "10111010101110",
        "11101110101010",
        "10101110101110",
        "11101011101010",
        "10111011101010",
        "10101011101110",
        "11101010111010",
        "10111010111010"
    };

    #endregion

    #region LinearBarcodeBase members

    /// <inheritdoc cref="LinearBarcodeBase.Encode"/>
    public override string Encode
        (
            BarcodeData data
        )
    {
        Sure.NotNull (data);

        var text = data.Message.ThrowIfNull();
        var builder = StringBuilderPool.Shared.Get();
        builder.EnsureCapacity (8 + 7 + text.Length * 14);

        builder.Append ("11011010");

        foreach (var c in text)
        {
            var digit = c - '0';
            builder.Append (_patterns[digit]);
        }

        builder.Append ("1101011");

        return builder.ReturnShared();
    }

    /// <inheritdoc cref="LinearBarcodeBase.Verify"/>
    public override bool Verify
        (
            BarcodeData data
        )
    {
        Sure.NotNull (data);

        var message = data.Message;

        if (string.IsNullOrWhiteSpace (message))
        {
            return false;
        }

        foreach (var c in message)
        {
            if (!char.IsDigit (c))
            {
                return false;
            }
        }

        return true;
    }

    /// <inheritdoc cref="LinearBarcodeBase.Symbology"/>
    public override string Symbology => "Standard 2 of 5";

    #endregion
}
